﻿#include "MirGameShopCell.h"
#include "../MirGraphics/MLibrary.h"
#include "../Settings.h"
#include "../MirSounds/SoundManager.h"
#include "MirMessageBox.h"
#include "../MirScenes/GameScene.h"
#include "../../Shared/Enums.h"
#include "../../Shared/Language.h"
#include "../Forms/Client.CMain.h"

using namespace Client::MirGraphics;
using namespace Client::MirNetwork;
using namespace Client::MirObjects;
using namespace Client::MirScenes;
using namespace Client::MirSounds;
using namespace Client::MirScenes::Dialogs;
namespace C = ClientPackets;

namespace Client::MirControls
{

    GameShopCell::GameShopCell()
    {
        Size tempVar(125, 146);
        setSize(&tempVar);
        setIndex(750);
        setLibrary(Libraries::Title);
        MouseLeave += [&] (o, e)
        {
            GameScene::Scene->DisposeItemLabel();
            GameScene::HoverItem = nullptr;
            ShowItem = nullptr;
        };

        nameLabel = new MirLabel();
        Size tempVar2(125, 15);
        nameLabel->setSize(&tempVar2);
        nameLabel->setDrawFormat(TextFormatFlags::HorizontalCenter);
        Point tempVar3(0, 13);
        nameLabel->setLocation(&tempVar3);
        nameLabel->setParent(this);
        nameLabel->setNotControl(true);
        Font tempVar4(Settings::FontName, 8.0F);
        nameLabel->setFont(&tempVar4);

        goldLabel = new MirLabel();
        Size tempVar5(95, 20);
        goldLabel->setSize(&tempVar5);
        goldLabel->setDrawFormat(TextFormatFlags::RightToLeft | TextFormatFlags::Right);
        Point tempVar6(2, 102);
        goldLabel->setLocation(&tempVar6);
        goldLabel->setParent(this);
        goldLabel->setNotControl(true);
        Font tempVar7(Settings::FontName, 8.0F);
        goldLabel->setFont(&tempVar7);

        gpLabel = new MirLabel();
        Size tempVar8(95, 20);
        gpLabel->setSize(&tempVar8);
        gpLabel->setDrawFormat(TextFormatFlags::RightToLeft | TextFormatFlags::Right);
        Point tempVar9(2, 81);
        gpLabel->setLocation(&tempVar9);
        gpLabel->setParent(this);
        gpLabel->setNotControl(true);
        Font tempVar10(Settings::FontName, 8.0F);
        gpLabel->setFont(&tempVar10);

        StockLabel = new MirLabel();
        Size tempVar11(40, 20);
        StockLabel->setSize(&tempVar11);
        Point tempVar12(53, 37);
        StockLabel->setLocation(&tempVar12);
        StockLabel->setParent(this);
        StockLabel->setNotControl(true);
        StockLabel->setForeColour(Color::Gray);
        Font tempVar13(Settings::FontName, 7.0F);
        StockLabel->setFont(&tempVar13);
        StockLabel->setText("STOCK:");

        stockLabel = new MirLabel();
        Size tempVar14(20, 20);
        stockLabel->setSize(&tempVar14);
        stockLabel->setDrawFormat(TextFormatFlags::HorizontalCenter);
        Point tempVar15(93, 37);
        stockLabel->setLocation(&tempVar15);
        stockLabel->setParent(this);
        stockLabel->setNotControl(true);
        Font tempVar16(Settings::FontName, 7.0F);
        stockLabel->setFont(&tempVar16);

        countLabel = new MirLabel();
        Size tempVar17(30, 20);
        countLabel->setSize(&tempVar17);
        countLabel->setDrawFormat(TextFormatFlags::Right);
        Point tempVar18(16, 60);
        countLabel->setLocation(&tempVar18);
        countLabel->setParent(this);
        countLabel->setNotControl(true);
        Font tempVar19(Settings::FontName, 7.0F);
        countLabel->setFont(&tempVar19);


        BuyItem = new MirButton();
        BuyItem->setIndex(778);
        BuyItem->setHoverIndex(779);
        BuyItem->setPressedIndex(780);
        Point tempVar20(42, 122);
        BuyItem->setLocation(&tempVar20);
        BuyItem->setLibrary(Libraries::Title);
        BuyItem->setParent(this);
        BuyItem->setSound(SoundList::ButtonA);
        BuyItem->Click += [&] (o, e)
        {
            BuyProduct();
        };

        PreviewItem = new MirButton();
        PreviewItem->setIndex(781);
        PreviewItem->setHoverIndex(782);
        PreviewItem->setPressedIndex(783);
        Point tempVar21(8, 122);
        PreviewItem->setLocation(&tempVar21);
        PreviewItem->setLibrary(Libraries::Title);
        PreviewItem->setParent(this);
        PreviewItem->setSound(SoundList::ButtonA);
        PreviewItem->setVisible(false);
        PreviewItem->Click += [&] (o, e)
        {
                GameScene::Scene->GameShopDialog->Viewer->Dispose();
                GameScene::Scene->GameShopDialog->Viewer = new GameShopViewer();
                GameScene::Scene->GameShopDialog->Viewer->setParent(GameScene::Scene->GameShopDialog);
                GameScene::Scene->GameShopDialog->Viewer->setVisible(true);
                Point tempVar22(151, 115);
                GameScene::Scene->GameShopDialog->Viewer->setLocation(this->getLocation()->X < 350 ? new Point(416, 115) : &tempVar22);
                GameScene::Scene->GameShopDialog->Viewer->ViewerItem = Item;
                GameScene::Scene->GameShopDialog->Viewer->UpdateViewer();
        };


        quantityUp = new MirButton();
        quantityUp->setIndex(243);
        quantityUp->setHoverIndex(244);
        quantityUp->setPressedIndex(245);
        quantityUp->setLibrary(Libraries::Prguse2);
        quantityUp->setParent(this);
        Point tempVar23(97, 56);
        quantityUp->setLocation(&tempVar23);
        quantityUp->setSound(SoundList::ButtonA);
        quantityUp->Click += [&] (o, e)
        {
            if (CMain::Shift)
            {
                Quantity += 10;
            }
            else
            {
                Quantity++;
            }
    
            if ((static_cast<Decimal>(Quantity * Item->Count) / Item->Info->StackSize) > 5)
            {
                Quantity = ((5 * Item->Info->StackSize) / Item->Count) > 99 ? Quantity = 99 : Quantity = static_cast<unsigned char>((5 * Item->Info->StackSize) / Item->Count);
            }
            if (Quantity >= 99)
            {
                Quantity = 99;
            }
            if (Item->Stock != 0 && Quantity > Item->Stock)
            {
                Quantity = static_cast<unsigned char>(Item->Stock);
            }
        };

        quantityDown = new MirButton();
        quantityDown->setIndex(240);
        quantityDown->setHoverIndex(241);
        quantityDown->setPressedIndex(242);
        quantityDown->setLibrary(Libraries::Prguse2);
        quantityDown->setParent(this);
        Point tempVar24(55, 56);
        quantityDown->setLocation(&tempVar24);
        quantityDown->setSound(SoundList::ButtonA);
        quantityDown->Click += [&] (o, e)
        {
    
            if (CMain::Shift)
            {
                Quantity -= 10;
            }
            else
            {
                Quantity--;
            }
    
            if (Quantity <= 1 || Quantity > 99)
            {
                Quantity = 1;
            }
        };

        quantity = new MirLabel();
        Size tempVar25(20, 13);
        quantity->setSize(&tempVar25);
        quantity->setDrawFormat(TextFormatFlags::HorizontalCenter | TextFormatFlags::VerticalCenter);
        Point tempVar26(74, 56);
        quantity->setLocation(&tempVar26);
        quantity->setParent(this);
        quantity->setNotControl(true);
        Font tempVar27(Settings::FontName, 8.0F);
        quantity->setFont(&tempVar27);



    }

    void GameShopCell::BuyProduct()
    {
        unsigned int CreditCost;
        unsigned int GoldCost;
        MirMessageBox *messageBox;

        if (Item->CreditPrice * Quantity <= GameScene::Credit)
        {
            CreditCost = Item->CreditPrice * Quantity;
            messageBox = new MirMessageBox(std::string::Format("Are you sure would you like to buy {1} x {0}({3}) for {2} Credits?", Item->Info->getFriendlyName(), Quantity, CreditCost, Item->Count), MirMessageBoxButtons::YesNo);
        }
        else
        { //Needs to attempt to pay with gold and credits
            if (GameScene::Gold >= (((Item->GoldPrice * Quantity) / (Item->CreditPrice * Quantity)) * ((Item->CreditPrice * Quantity) - GameScene::Credit)))
            {
                GoldCost = ((Item->GoldPrice * Quantity) / (Item->CreditPrice * Quantity)) * ((Item->CreditPrice * Quantity) - GameScene::Credit);
                CreditCost = GameScene::Credit;
                if (CreditCost == 0)
                {
                    messageBox = new MirMessageBox(std::string::Format("Are you sure would you like to buy {1} x {0}({3}) for {2} Gold?", Item->Info->getFriendlyName(), Quantity, GoldCost, Item->Count), MirMessageBoxButtons::YesNo);
                }
                else
                {
                    messageBox = new MirMessageBox(std::string::Format("Are you sure would you like to buy {1} x {0}({4}) for {2} Credit and {3} Gold?", Item->Info->getFriendlyName(), Quantity, CreditCost, GoldCost, Item->Count), MirMessageBoxButtons::YesNo);
                }
            }
            else
            {
                GameScene::Scene->ChatDialog->ReceiveChat(GameLanguage::LowGold, ChatType::System);

                delete messageBox;
                return;
            }

        }

        messageBox->YesButton->Click += [&] (o, e)
        {
            C::GameshopBuy *tempVar = new C::GameshopBuy();
            tempVar->GIndex = Item->GIndex;
            tempVar->Quantity = Quantity;
            Network::Enqueue(tempVar);

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
        };
        messageBox->NoButton->Click += [&] (o, e)
        {
        };
        messageBox->Show();

        delete messageBox;
    }

    void GameShopCell::OnMouseMove(MouseEventArgs *e)
    {
        MirImageControl::OnMouseMove(e);

        if (GameScene::HoverItem != nullptr && (Item->Info->Index != GameScene::HoverItem->Info->Index))
        {
            GameScene::Scene->DisposeItemLabel();
            GameScene::HoverItem = nullptr;
            ShowItem = nullptr;
        }

        if (ShowItem == nullptr && ItemDisplayArea != nullptr && ItemDisplayArea->Contains(CMain::MPoint))
        {
            ShowItem = new UserItem(Item->Info);
            ShowItem->MaxDura = Item->Info->Durability;
            ShowItem->CurrentDura = Item->Info->Durability;
            ShowItem->Count = Item->Count;
            GameScene::Scene->CreateItemLabel(ShowItem);
        }
        else if (ShowItem != nullptr && ItemDisplayArea != nullptr && !ItemDisplayArea->Contains(CMain::MPoint))
        {
            GameScene::Scene->DisposeItemLabel();
            GameScene::HoverItem = nullptr;
            ShowItem = nullptr;
        }
    }

    void GameShopCell::UpdateText()
    {
        nameLabel->setText(Item->Info->getFriendlyName());
        nameLabel->setText(nameLabel->getText().length() > 17 ? nameLabel->getText().substr(0, 17) : nameLabel->getText());
        nameLabel->setForeColour(GameScene::Scene->GradeNameColor(Item->Info->Grade));
        quantity->setText(std::to_string(Quantity));
//C# TO C++ CONVERTER TODO TASK: There is no C++ equivalent to 'ToString':
        goldLabel->setText((Item->GoldPrice * Quantity).ToString("###,###,##0"));
//C# TO C++ CONVERTER TODO TASK: There is no C++ equivalent to 'ToString':
        gpLabel->setText((Item->CreditPrice * Quantity).ToString("###,###,##0"));
        if (Item->Stock >= 99)
        {
            stockLabel->setText("99+");
        }
        if (Item->Stock == 0)
        {
            stockLabel->setText("∞");
        }
        else
        {
            stockLabel->setText(std::to_string(Item->Stock));
        }
        countLabel->setText(std::to_string(Item->Count));

        if (Item->Info->Type == ItemType::Mount || Item->Info->Type == ItemType::Weapon || Item->Info->Type == ItemType::Armour || Item->Info->Type == ItemType::Transform)
        {
            PreviewItem->setVisible(true);
            Point tempVar(75, 122);
            BuyItem->setLocation(&tempVar);
        }
    }

    void GameShopCell::DrawControl()
    {

        MirImageControl::DrawControl();

        if (Item == nullptr)
        {
            return;
        }

        UpdateText();

        Size *size = Libraries::Items->GetTrueSize(Item->Info->Image);
        Point *offSet = new Point((32 - size->Width) / 2, (32 - size->Height) / 2);

        Libraries::Items->Draw(Item->Info->Image, offSet->X + getDisplayLocation()->X + 12, offSet->Y + getDisplayLocation()->Y + 40);
        Point tempVar(offSet->X + getDisplayLocation()->X + 12, offSet->Y + getDisplayLocation()->Y + 40);
        ItemDisplayArea = new Rectangle(&tempVar, size);

        delete offSet;
    }

    void GameShopCell::Dispose(bool disposing)
    {
        MirImageControl::Dispose(disposing);

        Item = nullptr;
        GameScene::HoverItem = nullptr;
        ShowItem = nullptr;
    }

    GameShopViewer::GameShopViewer()
    {
        setIndex(785); // 314;
        setLibrary(Libraries::Title); // Libraries.Prguse2;
        Point tempVar(405, 108);
        setLocation(&tempVar);
        BeforeDraw += GameShopViewer_BeforeDraw;
        //Click += (o, e) =>
        //{
        //Visible = false;
        //};

        CloseButton = new MirButton();
        CloseButton->setHoverIndex(362);
        CloseButton->setIndex(361);
        Point tempVar2(230, 8);
        CloseButton->setLocation(&tempVar2);
        CloseButton->setLibrary(Libraries::Prguse);
        CloseButton->setParent(this);
        CloseButton->setPressedIndex(363);
        CloseButton->setSound(SoundList::ButtonA);
        CloseButton->Click += [&] (o, e)
        {
            setVisible(false);
        };

        WeaponImage = new MirAnimatedControl();
        WeaponImage->setAnimated(false);
        Point tempVar3(105, 160);
        WeaponImage->setLocation(&tempVar3);
        WeaponImage->setAnimationCount(6);
        WeaponImage->setAnimationDelay(150);
        WeaponImage->setIndex(0);
        WeaponImage->setLibrary(Libraries::Prguse);
        WeaponImage->setLoop(true);
        WeaponImage->setParent(this);
        WeaponImage->setUseOffSet(true);
        WeaponImage->setNotControl(true);
        WeaponImage2 = new MirAnimatedControl();
        WeaponImage2->setAnimated(false);
        Point tempVar4(105, 160);
        WeaponImage2->setLocation(&tempVar4);
        WeaponImage2->setAnimationCount(6);
        WeaponImage2->setAnimationDelay(150);
        WeaponImage2->setIndex(0);
        WeaponImage2->setLibrary(Libraries::Prguse);
        WeaponImage2->setLoop(true);
        WeaponImage2->setParent(this);
        WeaponImage2->setUseOffSet(true);
        WeaponImage2->setNotControl(true);
        MountImage = new MirAnimatedControl();
        MountImage->setAnimated(false);
        Point tempVar5(105, 160);
        MountImage->setLocation(&tempVar5);
        MountImage->setAnimationCount(8);
        MountImage->setAnimationDelay(150);
        MountImage->setIndex(0);
        MountImage->setLibrary(Libraries::Prguse);
        MountImage->setLoop(true);
        MountImage->setParent(this);
        MountImage->setUseOffSet(true);
        MountImage->setNotControl(true);

        PreviewImage = new MirAnimatedControl();
        PreviewImage->setAnimated(false);
        Point tempVar6(105, 160);
        PreviewImage->setLocation(&tempVar6);
        PreviewImage->setAnimationCount(6);
        PreviewImage->setAnimationDelay(150);
        PreviewImage->setIndex(0);
        PreviewImage->setLibrary(Libraries::Prguse);
        PreviewImage->setLoop(true);
        PreviewImage->setParent(this);
        PreviewImage->setUseOffSet(true);
        PreviewImage->setNotControl(true);

        RightDirection = new MirButton();
        RightDirection->setIndex(243);
        RightDirection->setHoverIndex(244);
        RightDirection->setPressedIndex(245);
        RightDirection->setLibrary(Libraries::Prguse2);
        RightDirection->setParent(this);
        Point tempVar7(160, 282);
        RightDirection->setLocation(&tempVar7);
        RightDirection->setSound(SoundList::ButtonA);
        RightDirection->Click += [&] (o, e)
        {
            Direction++;
            if (Direction > 8)
            {
                Direction = 1;
            }
    
            UpdateViewer();
        };

        LeftDirection = new MirButton();
        LeftDirection->setIndex(240);
        LeftDirection->setHoverIndex(241);
        LeftDirection->setPressedIndex(242);
        LeftDirection->setLibrary(Libraries::Prguse2);
        LeftDirection->setParent(this);
        Point tempVar8(81, 282);
        LeftDirection->setLocation(&tempVar8);
        LeftDirection->setSound(SoundList::ButtonA);
        LeftDirection->Click += [&] (o, e)
        {
            Direction--;
            if (Direction == 0)
            {
                Direction = 8;
            }
    
            UpdateViewer();
        };

    }

    void GameShopViewer::UpdateViewer()
    {
        this->setVisible(true);
        if (ViewerItem->Info->Type == ItemType::Weapon)
        {
            DrawWeapon();
        }
        if (ViewerItem->Info->Type == ItemType::Armour)
        {
            DrawArmour();
        }
        if (ViewerItem->Info->Type == ItemType::Mount)
        {
            DrawMount();
        }
        if (ViewerItem->Info->Type == ItemType::Transform)
        {
            DrawTransform();
        }
    }

    void GameShopViewer::GameShopViewer_BeforeDraw(std::any sender, EventArgs *e)
    {
        BringToFront();
    }

    void GameShopViewer::DrawMount()
    {
        WeaponImage->setVisible(false);
        WeaponImage2->setVisible(false);
        if (GameScene::getUser()->Equipment[static_cast<int>(EquipmentSlot::Armour)] != nullptr)
        {
            PreviewImage->setLibrary(Libraries::CArmours[GameScene::getUser()->Equipment[static_cast<int>(EquipmentSlot::Armour)]->Info.Shape]);
        }
        else
        {
            PreviewImage->setLibrary(Libraries::CArmours[0]);
        }

        if (GameScene::getUser()->Gender == MirGender::Male)
        {
            PreviewImage->setIndex(448 + (8 * (Direction - 1)));
        }
        else
        {
            PreviewImage->setIndex(1256 + (8 * (Direction - 1)));
        }

        PreviewImage->setAnimationCount(8);


        MountImage->setLibrary(Libraries::Mounts[ViewerItem->Info->Shape]);
        MountImage->setIndex(32 + (8 * (Direction - 1)));

        PreviewImage->setAnimated(true);
        MountImage->setAnimated(true);

        MountImage->setVisible(true);
    }

    void GameShopViewer::DrawWeapon()
    {
        MountImage->setVisible(false);

        if (GameScene::getUser()->Equipment[static_cast<int>(EquipmentSlot::Armour)] != nullptr)
        {
            PreviewImage->setLibrary(Libraries::CArmours[GameScene::getUser()->Equipment[static_cast<int>(EquipmentSlot::Armour)]->Info.Shape]);
        }
        else
        {
            PreviewImage->setLibrary(Libraries::CArmours[0]);
        }

        if (GameScene::getUser()->Gender == MirGender::Male)
        {
            PreviewImage->setIndex(32 + (6 * (Direction - 1)));
        }
        else
        {
            PreviewImage->setIndex(840 + (6 * (Direction - 1)));
        }




        if (Direction > 1 && Direction < 5)
        {
            WeaponImage->BringToFront();
        }
        else
        {
            PreviewImage->BringToFront();
        }


        if (ViewerItem->Info->Shape >= 100 && ViewerItem->Info->Shape <= 199)
        {
            WeaponImage->setLibrary(Libraries::AWeaponsR[ViewerItem->Info->Shape - 100]);
            WeaponImage2->setLibrary(Libraries::AWeaponsL[ViewerItem->Info->Shape - 100]);
            WeaponImage2->setVisible(true);
            WeaponImage2->setIndex(32 + (6 * (Direction - 1)));

            if (Direction >= 2 && Direction <= 3)
            {
                WeaponImage2->BringToFront();
                PreviewImage->BringToFront();
                WeaponImage->BringToFront();
            }
            else if (Direction == 8 || Direction == 7)
            {
                WeaponImage->BringToFront();
                PreviewImage->BringToFront();
                WeaponImage2->BringToFront();
            }
            else
            {
                WeaponImage->BringToFront();
                PreviewImage->BringToFront();
            }

        }
        else
        {
            WeaponImage2->setVisible(false);

        }
        if (ViewerItem->Info->Shape >= 200)
        {
            WeaponImage->setLibrary(Libraries::ARWeapons[ViewerItem->Info->Shape - 200]);
            if (Direction >= 6 && Direction <= 8)
            {
                PreviewImage->BringToFront();
                WeaponImage->BringToFront();
            }

        }

        if (ViewerItem->Info->Shape < 100)
        {
            WeaponImage->setLibrary(Libraries::CWeapons[ViewerItem->Info->Shape]);
        }


        WeaponImage->setIndex(32 + (6 * (Direction - 1)));

        PreviewImage->setAnimationCount(6);
        PreviewImage->setAnimated(true);
        WeaponImage2->setAnimated(true);
        WeaponImage->setAnimated(true);

        WeaponImage->setVisible(true);
    }

    void GameShopViewer::DrawArmour()
    {
        WeaponImage->setVisible(false);
        WeaponImage2->setVisible(false);
        MountImage->setVisible(false);

        if (ViewerItem->Info->RequiredGender == RequiredGender::Male)
        {
            PreviewImage->setIndex(32 + (6 * (Direction - 1)));
        }
        else
        {
            PreviewImage->setIndex(840 + (6 * (Direction - 1)));
        }

        PreviewImage->setLibrary(Libraries::CArmours[ViewerItem->Info->Shape]);
        PreviewImage->setAnimationCount(6);
        PreviewImage->setAnimated(true);

    }

    void GameShopViewer::DrawTransform()
    {
        WeaponImage->setVisible(false);
        WeaponImage2->setVisible(false);
        MountImage->setVisible(false);

        PreviewImage->setIndex(32 + (6 * (Direction - 1)));

        PreviewImage->setLibrary(Libraries::Transform[ViewerItem->Info->Shape]);
        PreviewImage->setAnimationCount(6);
        PreviewImage->setAnimated(true);

    }

    void GameShopViewer::DrawMask()
    {


    }
}
